package com.order.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.order.bean.User;
import com.order.service.UserService;
import com.order.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UserController", value = "/user/*")
public class UserController extends BaseController{

    private UserService userService = new UserServiceImpl();

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        User user = JSON.parseObject(json, User.class);
        User logined = userService.login(user);
        this.writeResultAndData(response, logined);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = request.getReader().readLine();

        User user = JSON.parseObject(json, User.class);

        boolean flag = userService.addUser(user);

        this.writeResult(response, flag);
    }

    protected void deleteUserById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));

        boolean flag = userService.deleteById(id);

        this.writeResult(response, flag);
    }

    protected void deleteUserBatch(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idsStr = request.getReader().readLine();
        Integer[] ids = JSON.parseObject(idsStr, Integer[].class);
        boolean flag = userService.deleteBatch(ids);
        this.writeResult(response, flag);
    }

    protected void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = request.getReader().readLine();
        User user = JSON.parseObject(json, User.class);
        boolean flag = userService.updateUser(user);
        this.writeResult(response, flag);
    }

    protected void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        User user = userService.selectUserById(id);
        this.writeResultAndData(response,user);
    }

    protected void selectUserList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = request.getReader().readLine();
        User user = JSON.parseObject(json, User.class);
        List<User> users = userService.selectAll(user.getUsername());
        this.writeResultAndData(response,users);
    }

    protected void selectUserPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        PageInfo<User> userPageInfo = userService.selectPage(name, pageNum, pageSize);
        this.writeResultAndData(response,userPageInfo);
    }
}
